import { Group } from "../model/group";
import { Team } from "../model/team";
import utils from "../utils/util";
import { GroupMax, TeamMax, MemberMax } from "../init/init";
class GroupManager{
    private groups:Array<Group>;
    private groupMap:Map<number, Group>;
    constructor(){
        this.groups = new Array<Group>();
        this.groupMap = new Map<number, Group>();
    }
    public add(group:Group){
        this.groupMap.set(group.getId(), group);
        this.groups.push(group);
    }
    public remove(id:number):boolean{
        if(this.groupMap.has(id)){
            for(let i=0; i<this.groups.length;++i){
                if(this.groups[i].getId()==id){
                    this.groups.splice(i,1);
                    break;
                }
            }
           return  this.groupMap.delete(id);
        }
        return false;
    }
    public createGroup():Group{
        let groupId = utils.randomId();
        let group = new Group(groupId);
       for(let i=0;i<TeamMax;++i){
            for(let j=0;j<MemberMax;++j){
                group.add(new Team(j));
            }
        }
        return group;
    }
    public getGroup(id:number):Group|null{
        if(this.groupMap.has(id)){
            return this.groupMap[id];
        }
        return null;
    }
    public getGroups():Array<Group>{
        return this.groups;
    }
    public getGroupCount():number{
        return this.getGroups().length;
    }
    public clear(){
        this.groups.length = 0;
        this.groupMap.clear();
    }
}

export const groupManager = new GroupManager();